package com.zhuiyun.project.api.addfriend.mapper;

import com.zhuiyun.project.api.addfriend.entity.AddFriend;
import com.zhuiyun.project.common.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

/**
 * @ClassName AddFriendMapper
 * @Description TODO 添加好友mapper
 * @Author kdj
 * @Date 2023/6/20 14:25
 **/
@Mapper
public interface AddFriendMapper extends BaseMapper<AddFriend> {
    /**
     * @param fromId
     * @param toId
     * @Author kdj
     * @Description 删除好友
     * @Date 2023/7/21 14:40
     * @Return int
     */
    @Update(" UPDATE add_friend" +
            "            SET del_flag=1 " +
            "            WHERE del_flag=0" +
            "            AND(( user_id = #{fromId} AND friend_id =  #{toId})" +
            "            OR (user_id =  #{toId} AND friend_id = #{fromId}))")
    int delFriend(Integer fromId, Integer toId);

    /**
    * @Author kdj
    * @Description  查询 我添加过或被添加过的记录
    * @Date 2023/8/9 19:43
    * @Return java.util.List<java.util.Map>
    * @param userId
    */
    @Select("select  t.* from(\n" +
            "select af.user_id userId, af.friend_id friendId,af.request_status status,af.update_time updateTime,\n" +
            " IF(length(af.rejection_reason)!=0,  af.rejection_reason , CONCAT('我：',af.request_message) ) AS message,\n" +
            "u.image_id AS imageId,u.user_name As userName,u.user_type  As userType ,user_sex As userSex,u.user_birthday As userBirthday\n" +
            ",CASE " +
            "   WHEN af.user_id = #{userId} THEN TRUE " +
            "   ELSE FALSE " +
            "   END AS request " +
            "from add_friend af \n" +
            "LEFT JOIN app_user u on  (u.id = af.friend_id AND af.user_id = #{userId})\n" +
            "where  af.user_id=#{userId}   and af.del_flag=0  and  u.del_flag=0\n" +
            "UNION ALL\n" +
            "select af.friend_id userId, af.user_id friendId,af.request_status status,af.update_time updateTime,\n" +
            " IF(length(af.rejection_reason)!=0,  CONCAT('我：',af.rejection_reason)   ,af.request_message ) AS message,\n" +
            "u.image_id AS imageId,u.user_name As userName,u.user_type  As userType ,user_sex As userSex,u.user_birthday As userBirthday\n" +
            ",CASE " +
            "   WHEN af.user_id = #{userId} THEN TRUE " +
            "   ELSE FALSE " +
            "   END AS request " +
            "from add_friend af \n" +
            "LEFT JOIN app_user u on (u.id = af.user_id AND af.friend_id =#{userId})  \n" +
            "where af.friend_id=#{userId} and af.del_flag=0 and   u.del_flag=0) t  order by t.updateTime desc;\n" +
            "\n" +
            " ")
    List<Map> getFriendRequestList(int userId);

    /**
    * @Author kdj
    * @Description 成为医师
    * @Date 2023/8/12 12:30
    * @Return void
    * @param doctorId
    */
    @Update(" UPDATE add_friend" +
            "            SET del_flag=1 " +
            "            WHERE del_flag=0 AND (user_id=#{id} OR friend_id=#{id})")
    void toPhy(Integer doctorId);
}
